$().ready(init);
function init(){
	(new jCT($('#jcttpl').html())).Build().ExtendTo(grid);
	var id=GID;
	$('body').html(grid.GetView(gridset,gridrows));
	jCTLib.grid(id,gridset);
}
function Gid(add){
	if(add) GID++;
	return GID;
}
var GID=1,grid={};
var gridset={
	fields:['a','b','c'],
	widths:['250px','20px','100px'],
	views:{
	}
}
var gridrows=[
	{a:1,b:'2009-09-09',c:'dfdfdf'},
	{a:2,b:'2009-09-09',c:'dfdfdf'},
	{a:3,b:'2009-09-09',c:'dfdfdf'},
	{a:4,b:'2009-09-09',c:'dfdfdf'},
	{a:5,b:'2009-09-09',c:'dfdfdf'}
	];
(function($){
	jCTLib={event:false}
	//每次调节保存的临时对象
	$(document)
	.mousemove(function(e){
		if(jCTLib.event.mousemove) jCTLib.event.mousemove(e);
	})
	.mouseup(function(e){
		if(jCTLib.event.mouseup) jCTLib.event.mouseup(e);
	});

	$.fn.noSelect = function() { //no select plugin from flexigrid
		return this.each(function ()
			{
				if ($.browser.msie||$.browser.safari) $(this).bind('selectstart',function(){return false;});
				else if ($.browser.mozilla) 
					{
						$(this).css('MozUserSelect','none');
						$('body').trigger('focus');
					}
				else if ($.browser.opera) $(this).bind('mousedown',function(){return false;});
				else $(this).attr('unselectable','on');
			});
	}; //end noSelect
})(jQuery);
jCTLib.grid=function(id,gridset){
	var grid=$('#jctgrid-'+id),widths=gridset?gridset.widths:[];
	if(typeof widths=='string'){
		grid.find('table').width(widths);
		widths=[];
	}//else		grid.find('table').css({width:'auto'});
	this.gridColFixWidth(grid,widths);
	//绑定scroll事件
	grid.find('.jctgrid-gbody').scroll(function(){
		$(this).siblings('.jctgrid-ghead').scrollLeft(this.scrollLeft);
	});
	//绑定col宽度调节事件
	grid.find('.jctgrid-colwidth').mousedown(function(e){
		var ths=$(this),cn=ths.attr('class'),index=cn.indexOf('index-');
		if(index==-1) return;
		index=parseInt(cn.slice(index+6));
		var grid=ths.closest('.jctgrid');
		jCTLib.event={
			mousemove:jCTLib.gridColReWidth,
			mouseup:jCTLib.gridColReWidthSet,
			col:grid.find('.jctgrid-colwidth.index-'+index),
			grid:grid,
			index:index,
			startX:e.pageX,
			diff:0,
			left:parseInt(ths.css('left'))
		};
	});
}
jCTLib.gridColFixWidth=function(grid,widths){
	widths=widths||[];
	//设置gbody宽度并获取实际px单位值
	grid.find('.jctgrid-gbody table thead td').each(function(i){
		if(widths[i]===undefined)
			widths[i]=$(this).width();
		else
			widths[i]=$(this).css({width:widths[i]}).width();
	});
	//设置ghead宽度
	grid.find('.jctgrid-ghead table td').each(function(i){
		widths[i]=$(this).css({width:widths[i]}).outerWidth(true);
	});
	//设置col宽度调节条
	var left=0;
	grid.find('.jctgrid-ghead .jctgrid-colwidth').each(function(i){
		left+=widths[i];
		$(this).css({left:left-1+'px'});
	});
	var left=0;
	grid.find('.jctgrid-gbody .jctgrid-colwidth').each(function(i){
		left+=widths[i];
		$(this).css({left:left-1+'px'});
	});
}
jCTLib.gridColReWidth=function(e){
	this.left+=e.pageX-this.startX;
	this.diff+=e.pageX-this.startX;
	this.startX=e.pageX;
	this.col.css({left:this.left+'px'});
	$('body').noSelect();
}
jCTLib.gridColReWidthSet=function(e){
	if(!this.diff) return;
	jCTLib.event=false;
	var n=this.index+1,cols=this.grid.find('.jctgrid-gbody table thead td:nth-child('+n+')');
	var w=cols.width()+this.diff;
	if(w<10) w=10;
	cols.css({width:w+'px'});
	jCTLib.gridColFixWidth(this.grid);
}